package sqldbManagement.dbparklot;

import sqldbManagement.dbparklot.cj.aop.Mapper;
import sqldbManagement.dbparklot.cj.model.Model;
import sqldbManagement.dbparklot.cj.utils.TimeUtil;
import sqldbManagement.dbparklot.entity.Park;
import sqldbManagement.dbparklot.mapper.ParkDao;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

    static List<Park> list;
    static ParkDao dao = Mapper.getMapper(ParkDao.class);

    public static void main(String[] args) {
        System.out.println("停车场，总停车位数量： 5 ");
        System.out.println("1. 停车");
        System.out.println("2. 查询费用");
        System.out.println("3. 查询全部停车位的状态");
        System.out.println("4. 取车");
        System.out.println("5. 查询全部计费记录");
        System.out.println("0. 退出");
        Scanner scanner = new Scanner(System.in);
        create();
        while(true){
            switch (scanner.nextInt()){
                case 1:
                    stop(scanner);
                    break;
                case 2:
                    queryPark(scanner);
                    break;
                case 3:
                    queryParkMsg(scanner);
                    break;
                case 4:
                    getCar(scanner);
                    break;
                case 5:
                    price(scanner);
                    break;
                case 0:
                	System.out.println("已经退出，请关闭程序。");
                	return;
            }
        }
    }

    private static void price(Scanner scanner) {
        List<Model> all = dao.getParkPriceAll();
        all.forEach(e -> {
            System.out.print("停车位序号："+e.get("car_park_id")+"\t 停车车牌："+e.get("name")+
                    "\t 开始时间：" + e.get("time")+ "\t 是否结束：");
            if(((double) e.get("price")) > 0)
                System.out.println("已结束\t 费用："+e.get("price"));
            else
                System.out.println("未结束");
        });
    }

    private static void getCar(Scanner scanner) {
        System.out.println("请输入车牌");
        String name = scanner.next();
        System.out.println("请输入停车位序号");
        int i = scanner.nextInt()-1;

        if(i > list.size()-1 || i < 0){
            System.out.println("不存在的停车位序号");
            return;
        }

        Park park = list.get(i);

        Model model = dao.getParkPrice(name);

        if(model == null){
            System.out.println("此车牌没有停车");
            return ;
        }

        Timestamp time = (Timestamp) model.get("time");

        BigDecimal price = TimeUtil.getPrice(time, TimeUtil.getNow(), 2);
        int i1 = dao.updateParkMsg(price, park.getId());

        if(i1 == 0){
            System.out.println("error");
            return ;
        }

        System.out.println("请缴费 " +price.doubleValue() + "元");
        dao.updatePark(false, park.getId());
        System.out.println("取车成功");

    }

    private static void queryParkMsg(Scanner scanner) {
        for (Park park : list) {
            System.out.println(park.getName()+ " 状态：" + (park.getPark()? "不空闲":"空闲"));
        }
    }

    private static void queryPark(Scanner scanner) {
        System.out.println("请输入车牌");
        String name = scanner.next();

        Model model = dao.getParkPrice(name);

        if(model == null){
            System.out.println("此车牌没有停车");
            return ;
        }

        Timestamp time = (Timestamp) model.get("time");

        System.out.println("费用： "+TimeUtil.getPrice(time, TimeUtil.getNow(), 2).doubleValue());

    }


    private static void stop(Scanner scanner) {
        System.out.println("请输入车牌");
        String name = scanner.next();
        System.out.println("请输入车高");
        int i = scanner.nextInt();

        if(i >= 3){
            System.out.println("车高不符");
            return ;
        }

        int index = -1;
        int flag = -1;

        for (int in = 0; in < list.size(); in++) {
            if(!list.get(in).getPark()){
                index = list.get(in).getId();
                flag = in;
                break;
            }
        }

        if(index == -1){
            System.out.println("目前没有空闲停车位");
            return;
        }

        Model model = dao.getParkPrice(name);
        if(model != null){
            System.out.println("请取车后再停");
            return;
        }

        int update = dao.updatePark(true, index);
        dao.insertParkMsg(index,name, TimeUtil.getNow());

        if(update > 0){
            list.get(flag).setPark(true);
            System.out.println("停车成功,停车位是 " + index + " ,序号为: " + (flag+1));
        }

    }

    private static void create() {
        list = dao.getList();
    }
}
